package com.zlsy.springbootdemo.encryption.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zhouliang
 * @date 2020/12/17 11:12
 */
@Slf4j
public class EncryptUtil {
  /**
   * 加密
   *
   * @param encryKey
   * @param charSet
   * @param content
   * @return
   */
  public static String encrypt(String encryKey, String charSet, String content) {
    if (StringUtils.isBlank(encryKey)) {
      throw new RuntimeException("[sec.encryption] is null");
    }
    // 解密
    try {
      byte[] key = Base64.decodeBase64(encryKey.getBytes(charSet));
      // content Base64解码
      byte[] inputData = content.getBytes(charSet);
      byte[] outputData = AESCoder.encrypt(inputData, key);
      content = new String(Base64.encodeBase64(outputData), charSet);
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
    return content;
  }
}
